﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using Dapper;
using Dapper.Contrib.Extensions;
using Org.BouncyCastle.Asn1.Mozilla;
using System.Deployment.Internal;
using Rh.Utility.Common;
using Z.Dapper.Plus;
using System.Transactions;
using System.Runtime.Remoting.Contexts;

namespace MVVMDemo
{
    public class CustomerModelViewModel: ViewModelBase, IDisposable
    {

        string connectionString = "server=localhost;port=3307;database=testdb;uid=root;pwd=root";

        IList<CustomerModel> _customers = null;     // 获取一个list集合
        
        // 当前的CustomerModel, 用于"新增/修改"窗口        
        public CustomerModel CurrentCustomerModel { get; set; }

        // 编辑状态：新增，编辑        
        public string EditStatus { get; set; }


        public CustomerModelViewModel()
        {
            DbContext = new MySqlConnection(connectionString);
        }

        public CustomerModelViewModel(CustomerModel customerModel)
        {
            CurrentCustomerModel = customerModel;
            DbContext = new MySqlConnection(connectionString);
        }

        /// <summary>
        /// 获取ViewModel的状态：新增、编辑
        /// </summary>
        /// <param name="customerModel"></param>
        public void GetEditStatus(CustomerModel customerModel)
        {
            if (CurrentCustomerModel.CustomerId == 0 || CurrentCustomerModel.CustomerId == null)
            {
                EditStatus = "（新增）";
            }
            else
            {
                EditStatus = "（编辑）";
            }
        }


        public BindingSource CustomerModelBindingSource { get; set; }

       

        public void Load()
        {  
            string sqlStr = "SELECT * FROM customer_excel";

            _customers = DbContext.Query<CustomerModel>(sqlStr).ToList();

            CustomerModelBindingSource.DataSource = _customers;
        }

        public void Delete(CustomerModel customerModel) 
        {
            string sqlStr = "SELECT * FROM customer_excel";
            var coustomers = DbContext.Delete<CustomerModel>(customerModel);

            CustomerModelBindingSource.RemoveCurrent();
        }

        public void New(CustomerModel newCustomerModel) {

            CustomerModelBindingSource.Add(newCustomerModel);
            //CustomerModelBindingSource.AddNew();
        } 
        public void Save()
        {
            CustomerModelBindingSource.EndEdit();
            //DbContext.Update<CustomerModel>();

        }


        public void SetID() 
        {
            IIdHelper idHelper = new IdHelper();

            foreach (CustomerModel item in _customers)
            {
                if (item.CustomerId >= 0 || item.CustomerId.ToString().Length != 18)
                {
                    item.CustomerId = long.Parse(idHelper.GetId(IdType.SnowId));
                    //DbContext.Update<CustomerModel>(item);
                }
            }

            // 批量更新需要以pk来更新，否则出错
            // 表中必须有pk，并且在model中设置pk的属性为[ExplicitKey]
            DbContext.BulkUpdate(_customers);
        }
    }
}
